package com.lemon.cloud.mp.web;

import com.alibaba.fastjson.JSONObject;
import com.lemon.cloud.comm.model.ResultMsg;
import com.lemon.cloud.log.annotation.SysLog;
import com.lemon.cloud.mp.service.WxMenuService;
import lombok.AllArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

/**
 * 微信菜单管理
 *
 * @author st
 * @date 2019-03-27 20:45:18
 */
@RestController
@AllArgsConstructor
@RequestMapping("/wxmenu")
public class WxMenuController {

    private final WxMenuService wxMenuService;

    /**
     * 通过appid查询微信菜单
     *
     * @param appId 公众号
     * @return ResultMsg
     */
    @GetMapping("/{appId}")
    public ResultMsg<String> getById(@PathVariable("appId") String appId) {
        return wxMenuService.getByAppId(appId);
    }

    /**
     * 新增微信菜单
     *
     * @param wxMenu 微信菜单列表
     * @return ResultMsg
     */
    @SysLog("新增微信菜单")
    @PostMapping("/{appId}")
    @PreAuthorize("hasAuthority('mp_wxmenu_add')")
    public ResultMsg<Boolean> save(@RequestBody JSONObject wxMenu, @PathVariable("appId") String appId) {
        return ResultMsg.resultSuccess(wxMenuService.save(wxMenu, appId));
    }

    /**
     * 发布微信菜单
     *
     * @param appId 公众号
     * @return ResultMsg
     */
    @SysLog("发布微信菜单")
    @PutMapping("/{appId}")
    @PreAuthorize("hasAuthority('mp_wxmenu_push')")
    public ResultMsg<Void> updateById(@PathVariable("appId") String appId) {
        return wxMenuService.push(appId);
    }

    /**
     * 删除
     * @param appId
     * @return
     */
    @DeleteMapping("/{appId}")
    @PreAuthorize("hasAuthority('mp_wxmenu_del')")
    public ResultMsg<Void> delete(@PathVariable("appId") String appId) {
        return wxMenuService.delete(appId);
    }

    /**
     * 同步微信菜单
     * @return
     */
    @PostMapping("/syncObj/{appId}")
    @PreAuthorize("hasAuthority('mp_wxmenu_sync')")
    public ResultMsg<String> syncMenu(@PathVariable("appId") String appId){
        return wxMenuService.syncMenu(appId);
    }
}
